package com.xiaoxie.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaoxie.common.constant.Constants;
import com.xiaoxie.common.redis.RedisManager;
import com.xiaoxie.system.annatotion.SetDS;
import com.xiaoxie.system.datasource.DynamicDataSourceManager;
import com.xiaoxie.system.domain.DictType;
import com.xiaoxie.system.domain.DictValue;
import com.xiaoxie.system.mapper.DictTypeMapper;
import com.xiaoxie.system.mapper.DictValueMapper;
import com.xiaoxie.system.service.DictService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DictServiceImpl implements DictService {
    @Resource
    private DictTypeMapper dictTypeMapper;
    @Resource
    private RedisManager redisManager;
    @Resource
    private DictValueMapper dictValueMapper;

    @Resource
    private DynamicDataSourceManager dynamicDataSourceManager;

    @Override
    public List<DictType> getAllDictTypes() {
        return dictTypeMapper.getAllDictTypes();
    }

    @Transactional
    @Override
    public void addDictType(DictType dictType) {
        if (dictTypeHasType(dictType.getType())) {
            throw new RuntimeException("字典类型已经存在！");
        }
        dictTypeMapper.insertDictType(dictType);
    }

    @Transactional
    @Override
    public void updateDictType(DictType dictType) {
        DictType oldDictType =  dictTypeMapper.getDictTypeById(dictType.getId());
        if (oldDictType == null) {
            throw  new RuntimeException("字典类型不存在！");
        }
        DictType db_dictType = dictTypeMapper.getDictTypeByType(dictType.getType());
        if(db_dictType != null && !db_dictType.getId().equals(dictType.getId())) {
            throw  new RuntimeException("字典类型已经存在！");
        }
        dictTypeMapper.updateDictType(dictType);
        List<DictValue> dictValues = dictValueMapper.getDictValuesByTypeId(dictType.getId());
        if (dictValues != null &&  !dictValues.isEmpty()) {
            redisManager.delete(Constants.DICT_PREFIX + oldDictType.getType());
            redisManager.setValue(Constants.DICT_PREFIX + dictType.getType(), dictValues);
        }
    }

    @Transactional
    @Override
    public void deleteDictTypeById(Integer id) {
        DictType dictType = dictTypeMapper.getDictTypeById(id);
        if (dictType == null) {
            throw new RuntimeException("字典类型不存在！");
        }
        redisManager.delete(Constants.DICT_PREFIX + dictType.getType());
        dictTypeMapper.deleteDictTypeById(id);
        dictValueMapper.deleteDictValueByTypeId(id);
    }

    @Override
    public PageInfo<DictType> selectDictTypeByPage(Integer pageNum, DictType dictType) {
        PageHelper.startPage(pageNum, Constants.PAGE_SIZE);
        List<DictType> dictTypes = dictTypeMapper.getDictTypeCondition(dictType);
        PageInfo<DictType> info = new PageInfo<>(dictTypes);
        return info;
    }

    @Override
    public boolean dictTypeHasType(String type) {
        // redis中判断键是否存在
        if (redisManager.countKeys(Constants.DICT_PREFIX + type) > 0) {
            return true;
        }
        int i = dictTypeMapper.countDictTypeByType(type);
        return i > 0;
    }

    @SetDS("test_db")
    @Override
    public DictType getDictTypeById(Integer id) {
        return dictTypeMapper.getDictTypeById(id);
    }

    @Transactional
    @Override
    public void batchDeleteDictType(List<Integer> ids) {
        // 获取这些项对应的DictType
        for (Integer id : ids) {
            DictType dictType = dictTypeMapper.getDictTypeById(id);
            if (dictType == null) {
                throw new RuntimeException("字典类型不存在！");
            }
            redisManager.delete(Constants.DICT_PREFIX + dictType.getType());
            dictTypeMapper.deleteDictTypeById(id);
            dictValueMapper.deleteDictValueByTypeId(id);
        }
    }

    @Override
    public List<Map<String, Object>> getDictTypeAndValueForExcel(DictType dictType) {
        return dictTypeMapper.selectDictExportData(dictType);
    }

}
